package com.a1anwang.okble.permission;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilityLifecycleCallbacks;
import ohos.aafwk.ability.AbilityPackage;
import ohos.app.Context;
import ohos.utils.PacMap;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;

/**
 * <pre>
 *     author:
 *                                      ___           ___           ___         ___
 *         _____                       /  /\         /__/\         /__/|       /  /\
 *        /  /::\                     /  /::\        \  \:\       |  |:|      /  /:/
 *       /  /:/\:\    ___     ___    /  /:/\:\        \  \:\      |  |:|     /__/::\
 *      /  /:/~/::\  /__/\   /  /\  /  /:/~/::\   _____\__\:\   __|  |:|     \__\/\:\
 *     /__/:/ /:/\:| \  \:\ /  /:/ /__/:/ /:/\:\ /__/::::::::\ /__/\_|:|____    \  \:\
 *     \  \:\/:/~/:/  \  \:\  /:/  \  \:\/:/__\/ \  \:\~~\~~\/ \  \:\/:::::/     \__\:\
 *      \  \::/ /:/    \  \:\/:/    \  \::/       \  \:\  ~~~   \  \::/~~~~      /  /:/
 *       \  \:\/:/      \  \::/      \  \:\        \  \:\        \  \:\         /__/:/
 *        \  \::/        \__\/        \  \:\        \  \:\        \  \:\        \__\/
 *         \__\/                       \__\/         \__\/         \__\/
 *     blog  : http://blankj.com
 *     time  : 16/12/08
 *     desc  : utils about initialization
 * </pre>
 */
public final class Utils {

    // @SuppressLint("StaticFieldLeak")
    private static AbilityPackage sApplication;

    private static final LinkedList<Ability> ABILITY_LIST = new LinkedList<>();

    private static AbilityLifecycleCallbacks mCallbacks = new AbilityLifecycleCallbacks() {
        @Override
        public void onAbilityStart(Ability ability) {
            setTopAbility(ability);
        }

        @Override
        public void onAbilityActive(Ability ability) {
            setTopAbility(ability);
        }

        @Override
        public void onAbilityInactive(Ability ability) {

        }

        @Override
        public void onAbilityForeground(Ability ability) {
            setTopAbility(ability);
        }

        @Override
        public void onAbilityBackground(Ability ability) {

        }

        @Override
        public void onAbilityStop(Ability ability) {
            ABILITY_LIST.remove(ability);
        }

        @Override
        public void onAbilitySaveState(PacMap pacMap) {

        }
    };

    private Utils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * Init utils.
     * <p>Init it in the class of Application.</p>
     *
     * @param context context
     */
    public static void init(final Context context) {
        init((AbilityPackage) context.getApplicationContext());
    }

    /**
     * Init utils.
     * <p>Init it in the class of Application.</p>
     *
     * @param app application
     */
    public static void init(final AbilityPackage app) {
        if (sApplication == null) {
            Utils.sApplication = app;
            Utils.sApplication.registerCallbacks(mCallbacks, null);
        }
    }

    /**
     * Return the context of Application object.
     *
     * @return the context of Application object
     * @noinspection checkstyle:JavadocMethod
     */
    public static AbilityPackage getApp() {
        if (sApplication != null) {
            return sApplication;
        }
        try {
            Class<?> abilityThread = Class.forName("ohos.app.AbilityThread");
            Object at = abilityThread.getMethod("currentAbilityThread").invoke(null);
            Object app = abilityThread.getMethod("getApplication").invoke(at);
            if (app == null) {
                throw new NullPointerException("u should init first");
            }
            init((AbilityPackage) app);
            return sApplication;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        throw new NullPointerException("u should init first");
    }

    private static void setTopAbility(final Ability ability) {
        if (ability.getClass() == PermissionUtils.PermissionAbility.class) {
            return;
        }
        if (ABILITY_LIST.contains(ability)) {
            if (!ABILITY_LIST.getLast().equals(ability)) {
                ABILITY_LIST.remove(ability);
                ABILITY_LIST.addLast(ability);
            }
        } else {
            ABILITY_LIST.addLast(ability);
        }
    }

    static LinkedList<Ability> getAbilityList() {
        return ABILITY_LIST;
    }
}
